package com.aiguigu.pool;

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        //ExecutorService threadPool= Executors.newCachedThreadPool();//一池N线程
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();


        //自定义线程池   Runtime.getRuntime().availableProcessors()获取cpu核数
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() + 1,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                new ThreadPoolExecutor.AbortPolicy()
        );

        try {
            for (int i = 0; i < 10; i++) {
                int finalI = i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"  "+ finalI);
                });
            }
        } catch (Exception e) {
                throw new RuntimeException(e);
        } finally {
            threadPool.shutdown();
        }

    }
}
